package com.example.administrator.hidedestext;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;

/**
 * Created by Chen qin lang on 2016/12/17.
 * 邮箱 :   619766577@qq.com
 */
public class MyAdapter extends BaseAdapter {
    public int num;
    public Context context;

    public MyAdapter(int total, Context context) {
        this.num = total;
        this.context = context;
    }

    @Override
    public int getCount() {
        return num;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView.setText("Hello World!\n用户信息 : 男 无业 \n 张三 18 性别 男\n  18" +
                " 性别 男\n 年龄 18 性别 男\n 年龄 18 性别 男\n 年龄 18das 性别 男");
        measureTextiw(holder.textView);
        return convertView;
    }
    public boolean isExtend=true;//判断动画类型
    class ViewHolder {
        TextView textView;
        public ViewHolder(View convertView){
            textView = (TextView) convertView.findViewById(R.id.item_tv);
            initClick();
        }

        private void initClick() {
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isExtend==true){
                        startAnimator(minHeight,maxHeight,isExtend);
                    }else {
                        startAnimator(maxHeight,minHeight,isExtend);
                    }
                }
            });
        }
        private void startAnimator(int start, int end, boolean isExtended) {
            ValueAnimator animator = ValueAnimator.ofInt(start,end);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int animatedValue = (int) animation.getAnimatedValue();
                    ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
                    layoutParams.height = animatedValue;
                    textView.setLayoutParams(layoutParams);
                }
            });
            animator.setDuration(500);
            animator.start();
            isExtend=!isExtended;
        }
    }

    public int minHeight;
    public int maxHeight;
    public void measureTextiw(final TextView textView) {

        //设置textVIew的固定高度
        textView.setMaxLines(1);
        textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                //获得最小高度
                minHeight = textView.getHeight();
                //设置最大高度
                textView.setMaxLines(Integer.MAX_VALUE);
                textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        //获得最大高度
                        maxHeight = textView.getHeight();
                        ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
                        layoutParams.height = minHeight;
                        //设置textView为最小高度
                        textView.setLayoutParams(layoutParams);
                    }
                });
            }
        });
    }
}
